/**
 * @author Eldar
 * @copyright Hire-Experts LLC
 * @version Contacts Module 1.02
 */

var he_contacts = {

    callback_url : '',
    get_contacts_url : 'he_contacts.php?',
    message_allowed : 0,
    emails_allowed : 0,
    contacts : [],
    last : 0,
    title : '',
    callback_function : '',

    init : function( callback_url, message_allowed, last, get_contacts_url, emails_allowed, title, callback_function ) {
        this.callback_url = callback_url;
        this.message_allowed = message_allowed;
        this.last = last;
        this.title = title;
        this.emails_allowed = emails_allowed;
        if( get_contacts_url ) this.get_contacts_url = get_contacts_url;
        this.callback_function = callback_function;
        
        this.init_check_all();
    },

    link : function( title, callback_url, message_allowed ) {
        javascript:TB_show(title, this.get_contacts_url + 'callback_url=' + callback_url + '&message_allowed=' + message_allowed + '&TB_iframe=true&height=460&width=580', '', './images/trans.gif');
    },
    
    open_form : function() {
        javascript:TB_show(this.title, this.get_contacts_url + 'callback_url=' + urlencode(this.callback_url) + '&message_allowed=' + this.message_allowed + '&emails_allowed=' + this.emails_allowed + '&TB_iframe=true&height=460&width=580', '', './images/trans.gif');
    },

    get_more_contacts : function() {
        var self = this;
        if( self.last == -1 ) return;

        $('he_contacts_loading').setStyle('display', 'block');
        overlay_size('he_contacts_loading');
        
        new Request.JSON({
            method: 'get',
            url: self.get_contacts_url + 'is_ajax=1&start=' + this.last,
            onSuccess: function(response) {
                $('he_contacts_loading').setStyle('display', 'none');
                if( response.html_code )
                {
                    var newDiv = document.createElement('div');
                    newDiv.innerHTML = response.html_code;
                    $('he_contacts_list').insertBefore(newDiv, document.getElementById('he_contacts_end_line'));
                }

                self.last = response.start;
                if( !response.more )
                {
                    $('he_contacts_more').set('class', 'more_disabled');
                    self.last = -1;
                }
            }
        }).send();
    },

    choose_contact : function( contact_id ) {
        if( this.contacts.indexOf(contact_id)==-1 ) { //add contact
            $("contact_" + contact_id).addClass("active");
            this.contacts[this.contacts.length] = contact_id;
        }
        else { //remove contact
            $("contact_" + contact_id).removeClass("active");
            this.contacts.splice(this.contacts.indexOf(contact_id), 1);
        }
    },

    send : function() {
        var self = this;
        $('he_contacts_loading').setStyle('display', 'block');
        overlay_size('he_contacts_loading');
        var post_data = {'contacts_choosed': 1 , 'contacts': self.contacts.toString()}
        if( this.emails_allowed ) post_data.emails = $('he_contacts_emails').value;
        
        new Request.JSON({
            method: 'get',
            url: this.callback_url,
            data: post_data,
            onSuccess: function(response) {
                $('he_contacts_loading').setStyle('display', 'none');
                $('he_contacts_message').setStyle('display', 'block');
                overlay_size('he_contacts_message');
                $('he_contacts_message').getElement('.msg').innerHTML = response.message;
                if( response.status ) {
                	if (typeof(self.callback_function) == "function")
                	{
                		self.callback_function();
                	}
                    setTimeout("parent.TB_remove();", 2500);
                }
                else {
                    setTimeout("$('he_contacts_message').setStyle('display', 'none');", 2500);
                }
            }
        }).send();
    },
    
    init_check_all : function() {
    	this.$check_all = $('he_contacts_check_all');
    	
    	if (!this.$check_all) {
			return false;
		}
    	
    	this.$check_all.addEvent('mousedown', function() {
    		if (this.hasClass('he_checked')) {
				this.removeClass('he_checked');
				this.addClass('he_before_uncheck');
			} else {
				this.removeClass('he_unchecked');
				this.addClass('he_before_check');
			}
    	});
    	
    	this.$check_all.addEvent('mouseup', function() {
    		if (this.hasClass('he_before_uncheck')) {
    			this.removeClass('he_before_uncheck');
    			this.addClass('he_unchecked');
    		} else {
    			this.removeClass('he_before_check');
    			this.addClass('he_checked');
    		}
    	});
    },
    
    check_all : function() {
    	var self = this;
		var $items = $$('#he_contacts_list .item');
		
		if (!this.$check_all || !$items || $items.length == 0) {
			return false;
		}
		
		$items.removeClass('active');
		this.contacts = [];
		
		if (this.$check_all.hasClass('he_checked')) {
			$items.each(function($item, index){
			    var item_id = $item.getProperty('id').substr(8).toInt();
			    self.contacts.push(item_id);
			});
			
			$items.addClass('active');
		}
    }
}
